package com.happy016.sender;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class HelloSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * Direct Exchange Rabbitmq默认的交换机模式,根据key全文匹配
     * @author Linyb
     * @since 2017/11/22 15:11
     */
    public void sendHello() {

        //模拟两个队列,各发送5条
        for(int i = 0 ;i<10; i++){

            String context = "Hello "+i+"!" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            System.out.println("Sender "+i+": " + context);

            //模拟两个队列
            String routingKey = (i % 2 == 0) ? "hello" : "test";
            this.rabbitTemplate.convertAndSend(routingKey, context);

        }
        //线程等待5秒,已便接收监听那边全部监听完成,这里测试情况特殊
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
